package com.sunseagear.wind.modules.oss.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sunseagear.common.base.http.R;
import com.sunseagear.common.base.http.RPage;
import com.sunseagear.common.base.mvc.controller.BaseBeanController;
import com.sunseagear.common.oss.exception.FileNameLengthLimitExceededException;
import com.sunseagear.common.oss.exception.InvalidExtensionException;
import com.sunseagear.common.utils.ObjectUtils;
import com.sunseagear.wind.common.aspectj.annotation.Log;
import com.sunseagear.wind.common.aspectj.enums.LogType;
import com.sunseagear.wind.common.helper.AttachmentHelper;
import com.sunseagear.wind.common.response.ResponseError;
import com.sunseagear.wind.modules.oss.entity.Attachment;
import com.sunseagear.wind.modules.oss.entity.query.AttachmentQuery;
import com.sunseagear.wind.modules.oss.service.IAttachmentService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.apache.commons.fileupload.FileUploadBase;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 *
 * @version V1.0
 * @package com.sunseagear.oss.controller
 * @title: 附件管理控制器
 * @description: 附件管理控制器 * @date: 2018-04-25 14:25:55
 * @copyright: 2018 www.sunseagear.com Inc. All rights reserved.
 */
@RestController
@RequestMapping("oss/attachment")
@Log(title = "附件日志")
@RequiredArgsConstructor
public class AttachmentController extends BaseBeanController {

    private final IAttachmentService attachmentService;

    private final AttachmentHelper attachmentHelper;

    /**
     * 根据页码和每页记录数，以及查询条件动态加载数据
     */
    @GetMapping(value = "list")
    @Log(logType = LogType.SELECT)
    @SaCheckPermission("oss:attachment:list")
    public RPage<Attachment> list(AttachmentQuery query) {
        //加入条件
        QueryWrapper<Attachment> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByDesc(Attachment::getUploadTime)
                .like(ObjectUtils.isNotEmpty(query.getFileName()), Attachment::getFileName, query.getFileName());
        // 预处理
        Page<Attachment> pageBean = attachmentService.page(getPage(), queryWrapper);
        return RPage.success(pageBean);
    }

    /**
     * 删除
     * @param idList 主键集合
     * @return 操作结果
     */
    @DeleteMapping("delete")
    @Log(logType = LogType.DELETE)
    @SaCheckPermission("oss:attachment:delete")
    public R<Void> delete(@RequestBody List<Long> idList) {
        attachmentService.removeBatchByIds(idList);
        return R.success("删除成功");
    }

    /**
     * Upload 方法
     * @param request 请求方法
     * @param file 文件
     * @param dir 请求方法
     * @return 文件路径
     */
    @RequestMapping(value = "upload", method = RequestMethod.POST)
    @SaCheckLogin
    public R<Object> upload(HttpServletRequest request, MultipartFile[] file, @RequestParam(required = false, defaultValue = "") String dir) {
        try {
            return R.success((Object) attachmentHelper.upload(request, file, dir));
        } catch (IOException | FileNameLengthLimitExceededException | FileUploadBase.FileSizeLimitExceededException |
                 InvalidExtensionException e) {
            return R.fail(ResponseError.NORMAL_ERROR, "服务器故障");
        }
    }


}
